﻿import com.dinmax.isolution.*

class WindowDeviceGUI
extends AppWindowGUI {
	
	//variables de dibujo
	private var mcContainer:MovieClip;
	
	private var activeDevice:MovieClip;
	private var devices:Array;
	
	public function showContent(){
		super.showContent();
		//trace("WindowDeviceGUI | pidiendo los devices al HCC");
				
		this.setDevices(HCC.getDevices());
				
		var nY:Number = 15;
		
		//itero en los devices
		for(var i:Number = 0; i < devices.length; i ++){
			var device:Device = devices[i];
			if(device instanceof LightDimmer) {
				
				//trace("WindowDeviceGUI | showContent | creo un dimmer");
				
				var nInstance:Number=0;
				while(this.mcContainer["mcDimmer"+nInstance]){
					nInstance++;
				}
				
				var dimmer:LightDimmerGUI = LightDimmerGUI(this.mcContainer.attachMovie("LightDimmerGUI", "mcDimmer"+nInstance, this.mcContainer.getNextHighestDepth()));
				dimmer._x = 15;
				dimmer._y = nY;
				nY += dimmer._height + 5;
				dimmer.setDevice(LightDimmer(device));
				
				var root:WindowDeviceGUI = this;
				dimmer.onPress = function(){
					this.setSelected(!this.getSelected());
					if(root.activeDevice != this) root.activeDevice.setSelected(false);
					root.activeDevice = this;
				}
				
			} else if(device instanceof LightRGB) {
				
				//trace("WindowDeviceGUI | showContent | creo un rgb");
				
				var nInstance:Number=0;
				while(this.mcContainer["mcRGB"+nInstance]){
					nInstance++;
				}
				
				var lrgb:LightRGBGUI = LightRGBGUI(this.mcContainer.attachMovie("LightRGBGUI", "mcRGB"+nInstance, this.mcContainer.getNextHighestDepth()));
				lrgb._x = 15;
				lrgb._y = nY;
				nY += lrgb._height + 5;
				lrgb.setDevice(LightRGB(device));
				
				var root:WindowDeviceGUI = this;
				lrgb.onPress = function(){
					this.setSelected(!this.getSelected());
					if(root.activeDevice != this) root.activeDevice.setSelected(false);
					root.activeDevice = this;
				}
				
			} else if(device instanceof Door) {
				
				var nInstance:Number=0;
				while(this.mcContainer["mcDoor"+nInstance]){
					nInstance++;
				}
				
				var door:DoorGUI = DoorGUI(this.mcContainer.attachMovie("DoorGUI", "mcDoor"+nInstance, this.mcContainer.getNextHighestDepth()));
				
				door._x = 15;
				door._y = nY;
				nY += door._height + 5;
				door.setDevice(Door(device));
				
				var root:WindowDeviceGUI = this;
				door.onPress = function(){
					this.setSelected(!this.getSelected());
					if(root.activeDevice != this) root.activeDevice.setSelected(false);
					root.activeDevice = this;
				}
			} else if(device instanceof Blind) {
				
				var nInstance:Number=0;
				while(this.mcContainer["mcBlind"+nInstance]){
					nInstance++;
				}
				
				var blind:BlindGUI = BlindGUI(this.mcContainer.attachMovie("BlindGUI", "mcBlind"+nInstance, this.mcContainer.getNextHighestDepth()));
				
				blind._x = 15;
				blind._y = nY;
				nY += blind["mcBack"]._height + 5;
				blind.setDevice(Blind(device));
				
				var root:WindowDeviceGUI = this;
				blind.onPress = function(){
					this.setSelected(!this.getSelected());
					if(root.activeDevice != this) root.activeDevice.setSelected(false);
					root.activeDevice = this;
				}
			}
			
		}
		//trace("WindowDeviceGUI | debo mostrar los devices al HCC");
		return;
	}
	
	public function showDeviceView(device:Device):Void {
		
		if(device instanceof LightDimmer){
			var editor = this["mcEditor"].attachMovie("LightDimmerEditorGUI", "mcEditor", 1);
			editor.setDevice(device);
			
		} else if(device instanceof LightRGB) {
			var editor = this["mcEditor"].attachMovie("LightRGBEditorGUI", "mcEditor", 1);
			editor.setDevice(device);
			
		} else if(device instanceof Door) {
			var editor = this["mcEditor"].attachMovie("DoorEditorGUI", "mcEditor", 1);
			editor.setDevice(device);
			
		} else if(device instanceof Blind) {
			var editor = this["mcEditor"].attachMovie("BlindEditorGUI", "mcEditor", 1);
			editor.setDevice(device);
			
		} 
		
		return;
	}
	
	public function getActiveDevice():MovieClip {
		return this.activeDevice;
	}
	
	public function setDevices(devices:Array):Void {
		this.devices = devices;
		return;
	}
	
	public function updateDevice(dev:Device):Void {
		// TODO esto está re choto hecho
		
		var captured:Boolean = false;
				
		var nInstance:Number = 0;
		while(this.mcContainer["mcDimmer"+nInstance]){
			if(this.mcContainer["mcDimmer"+nInstance].getDevice().getId() == dev.getId()){
				this.mcContainer["mcDimmer"+nInstance].setDevice(dev);
				captured = true;
			}
			nInstance++;
		}
		
		if(!captured){
			var nInstance:Number = 0;
			while(this.mcContainer["mcRGB"+nInstance]){
				if(this.mcContainer["mcRGB"+nInstance].getDevice().getId() == dev.getId()){
					this.mcContainer["mcRGB"+nInstance].setDevice(dev);
					captured = true;
				}
				nInstance++;
			}
		}
		
		if(!captured){
			var nInstance:Number = 0;
			while(this.mcContainer["mcDoor"+nInstance]){
				if(this.mcContainer["mcDoor"+nInstance].getDevice().getId() == dev.getId()){
					this.mcContainer["mcDoor"+nInstance].setDevice(dev);
					captured = true;
				}
				nInstance++;
			}
		}
		
		if(!captured){
			var nInstance:Number = 0;
			while(this.mcContainer["mcBlind"+nInstance]){
				if(this.mcContainer["mcBlind"+nInstance].getDevice().getId() == dev.getId()){
					this.mcContainer["mcBlind"+nInstance].setDevice(dev);
					captured = true;
				}
				nInstance++;
			}
		}
			
		
		if(this["mcEditor"].mcEditor.getDevice().getId() == dev.getId()){
			this["mcEditor"].mcEditor.setDevice(dev);
		}
		return;
	}
	
}